package com.ityedao.controller;

import com.ityedao.pojo.Result;
import com.ityedao.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;
//    @PostMapping("/upload")
//    public Result upload(
//            String username, Integer age, @RequestParam("image") MultipartFile image)
//            throws IOException {
//        log.info("文件上传: {},{},{}", username,age,image);
//
//        // 获取原始文件名
//        String originalFilename = image.getOriginalFilename();
//
//        // 构建新的文件名
//        String extname = originalFilename.substring(originalFilename.lastIndexOf(".")); // 文件扩展名
//        String newFileName = UUID.randomUUID().toString() + extname;
//
//        // 将文件存储在服务器的磁盘目录
//        image.transferTo(new File(new File("D:/code/") +
//                originalFilename));
//
//        return Result.success();
//    }

    // 修改代码
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传，文件名:{}", image.getOriginalFilename());
        // 调用阿里云OSS工具类进行文件上传
        String url = aliOSSUtils.upload(image);
        return Result.success(url);
    }

}
